﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.ChucNang
{
    public partial class BienBanNhapKhoLookup : FormPopupBase
    {

        private readonly BienBanNhapHangBO service = new BienBanNhapHangBO();
        public BienBanNhapHang BienBanNhapHangSelected;
        public BienBanNhapKhoLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvBienBanNhapHangList.OptionsBehavior.Editable = false;
            grvBienBanNhapHangList.OptionsView.ShowGroupPanel = false;
            grvBienBanNhapHangList.OptionsView.ShowIndicator = true;

            grvBienBanNhapHangList.OptionsView.ShowAutoFilterRow = true;
            grvBienBanNhapHangList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvBienBanNhapHangList.OptionsView.ShowGroupedColumns = false;
            grvBienBanNhapHangList.OptionsMenu.EnableColumnMenu = false;
            grvBienBanNhapHangList.OptionsCustomization.AllowColumnMoving = false;
            grvBienBanNhapHangList.OptionsCustomization.AllowGroup = false;
            grvBienBanNhapHangList.OptionsFilter.AllowFilterEditor = false;

            this.grvBienBanNhapHangList.OptionsSelection.MultiSelect = false;
            this.grvBienBanNhapHangList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaBBNK.FieldName = BienBanNhapHangColumns.MaBBNH.ToString();
            this.colSoHopDong.FieldName = BienBanNhapHangColumns.SoHopDong.ToString();
            this.colNoiNhap.FieldName = BienBanNhapHangColumns.NoiNhan.ToString();
            this.colKeToan.FieldName = BienBanNhapHangColumns.KeToan.ToString();
            this.colThuKho.FieldName = BienBanNhapHangColumns.ThuKho.ToString();
            this.colNguoiDuyet.FieldName = BienBanNhapHangColumns.NguoiDuyet.ToString();
            this.colNgayGioNhap.FieldName = BienBanNhapHangColumns.NgayGioNhan.ToString();
            this.colNgayGioKiem.FieldName = BienBanNhapHangColumns.NgayGioKiem.ToString();
  


        }

        void InitData()
        {
            try
            {
                
                    this.grdBienBanNhapHangList.DataSource = service.GetAllBienBanNhapHang();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.BienBanNhapHangSelected = (BienBanNhapHang)grvBienBanNhapHangList.GetFocusedRow();
                }
                else
                {
                    this.BienBanNhapHangSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvBienBanNhapHangList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvBienBanNhapHangList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdBienBanNhapHangList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (BienBanNhapHangSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvBienBanNhapHangList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvBienBanNhapHangList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            BienBanNhapHangSelected = this.grvBienBanNhapHangList.GetFocusedRow() as BienBanNhapHang;
        }

        private void grvBienBanNhapHangList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
